package swu.cx.finalproject

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.animation.*
import android.widget.ImageView
import kotlinx.android.synthetic.main.activity_show_picture.*

class show_picture : AppCompatActivity() {
    var count:Int=1
    var lastPic: ImageView?=null
    val sortId:MutableList<Int> = mutableListOf()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_show_picture)
        generateSort()
        back.setOnClickListener {
           finish()
        }
    }
    fun generateSort(){
        var i=0
        while (i<10){
            val num=(1..10).random()
            if (i>0) {
                for (j in 0 until i) {
                    if (sortId[j] == num) {
                        i--
                        break
                    }
                    if (j == (i-1)) {
                        sortId.add(num)
                    }
                }
            }else{
                sortId.add(num)
            }
            i++
        }
    }

    override fun onResume() {
        super.onResume()
        showPhotos()
    }
    fun showPhotos(){
        var animationSet= AnimationSet(true).apply {
            duration=2000
        }
        val id=sortId[count-1]
        val v=pContainer.findViewWithTag<ImageView>("$id")
        lastPic=v
        when(id%5){
            1->{
                when(id){
                    1->{
                        val translateAnimation=translateXY(-1.5f,0f,0f,0f)
                        animationSet.addAnimation(translateAnimation)
                        myaddAnim(animationSet, isrotate = false,isAlpha = true)
                    }
                    6->{
                        val translateAnimation=translateXY(1.5f,0f,0f,0f)
                        animationSet.addAnimation(translateAnimation)
                        myaddAnim(animationSet,isrotate = false,isAlpha = false)
                    }
                }
            }
            2->{
                when(id){
                    2->{
                        val translateAnimation=translateXY(0f,0.31f,0f,1.2f)
                        animationSet.addAnimation(translateAnimation)
                        myaddAnim(animationSet, isrotate = false,isAlpha = true)
                    }
                    7->{
                        val translateAnimation=translateXY(0f,0.31f,0f,1.2f)
                        animationSet.addAnimation(translateAnimation)
                        myaddAnim(animationSet,isrotate = true, isAlpha = false)
                    }
                }
            }
            3->{
                when(id){
                    3->{
                        val translateAnimation=translateXY(0f,-0.31f,0f,1.2f)
                        animationSet.addAnimation(translateAnimation)
                        myaddAnim(animationSet, isrotate = false,isAlpha = true)
                    }
                    8->{
                        val translateAnimation=translateXY(0f,-0.31f,0f,1.2f)
                        animationSet.addAnimation(translateAnimation)
                        myaddAnim(animationSet, isrotate = true,isAlpha = true)
                    }
                }
            }
            4->{
                when(id){
                    4->{
                        val translateAnimation=translateXY(0f,-0.31f,0f,-1.35f)
                        animationSet.addAnimation(translateAnimation)
                        myaddAnim(animationSet, isrotate = true,isAlpha = true)
                    }
                    9->{
                        val translateAnimation=translateXY(0f,-0.32f,0f,-1.4f)
                        animationSet.addAnimation(translateAnimation)
                        myaddAnim(animationSet, isrotate = false,isAlpha = true)
                    }
                }
            }
            0->{
                when(id){
                    5->{
                        val translateAnimation=translateXY(0f,0.31f,0f,-1.35f)
                        animationSet.addAnimation(translateAnimation)
                        myaddAnim(animationSet, isrotate = false,isAlpha = true)
                    }
                    10->{
                        val translateAnimation=translateXY(0f,0.31f,0f,-1.35f)
                        animationSet.addAnimation(translateAnimation)
                        myaddAnim(animationSet, isrotate = true,isAlpha = false)
                    }
                }
            }
        }
        v.startAnimation(animationSet)
    }
    fun myaddAnim(animationSet: AnimationSet, isrotate:Boolean, isAlpha:Boolean){
        val scaleAnimation=scalePhoto()
        val rotate=rotatePhoto()
        val alphaAnimation= AlphaAnimation(0f,1.0f)
        animationSet.addAnimation(scaleAnimation)
        if (isAlpha) animationSet.addAnimation(alphaAnimation)
        if (isrotate) animationSet.addAnimation(rotate)
        finishAnim(scaleAnimation,animationSet)
    }
    fun finishAnim(animation: Animation, animationSet: AnimationSet){
        animation.setAnimationListener(object :Mylistener(){
            override fun onAnimationEnd(animation: Animation?) {
                animationSet.addAnimation(alphaPhoto(animationSet))
            }
        })
    }
    fun scalePhoto()= ScaleAnimation(
        0.2f,2.0f,0.2f,2.0f,
        Animation.RELATIVE_TO_SELF,0.5f,
        Animation.RELATIVE_TO_SELF,0.5f
    )
    fun rotatePhoto()= RotateAnimation(
        0f,360f,
        Animation.RELATIVE_TO_SELF,0.5f,
        Animation.RELATIVE_TO_SELF,0.5f
    )
    fun alphaPhoto(animationSet: AnimationSet): AlphaAnimation {
        val animation= AlphaAnimation(1.0f, 0f).apply {
            duration = 1000
        }
        animation.setAnimationListener(object : Mylistener() {
            override fun onAnimationEnd(animation: Animation?) {
                lastPic?.alpha=0f
                count++
                if (count==11){
                    finalShow()
                    return
                }
               showPhotos()
            }
        })
        return animation
    }
    fun finalShow(){
        val fianlViews:Array<ImageView> = arrayOf(beauty1,beauty10,beauty6)
        for ((i,v) in fianlViews.withIndex()){
            v.visibility= View.VISIBLE
            v.alpha=1.0f
            when(i){
                0->{
                    finalView(-240f, -240f, 400f, -158f, v)
                }
                1->{
                    finalView(550f,550f,-400f,-1080f,v)
                }
                2-> {
                    finalView(0f, 0.1f, 0f, -560f, v)
                }
            }
        }
    }
    fun finalDeal(){
        lastshow.visibility=View.VISIBLE
            back.visibility=View.VISIBLE
    }
    fun finalView(tox1:Float,tox2:Float,toy1:Float,toy2:Float,v: ImageView){
        val movex= ObjectAnimator.ofFloat(v,"translationX",0f,tox1,tox2)
        val movey= ObjectAnimator.ofFloat(v,"translationY",0f,toy1,toy2)
        val scalex= ObjectAnimator.ofFloat(v,"scaleX",0.2f,2.0f,1.0f)
        val scaley= ObjectAnimator.ofFloat(v,"scaleY",0.2f,2.0f,1.0f)
        AnimatorSet().apply {
            playTogether(scalex,scaley,movex,movey)
            duration=2000
            start()
            if (v.tag=="6"){
               addListener(object :Animator.AnimatorListener{
                   override fun onAnimationStart(p0: Animator?) {
                   }
                   override fun onAnimationEnd(p0: Animator?) {
                       finalDeal()
                   }
                   override fun onAnimationCancel(p0: Animator?) {
                   }
                   override fun onAnimationRepeat(p0: Animator?) {
                   }
               })
            }
        }
    }
    fun translateXY(fromx: Float,tox: Float,fromy: Float,toy: Float)= TranslateAnimation(
        Animation.RELATIVE_TO_SELF,fromx,
        Animation.RELATIVE_TO_SELF,tox,
        Animation.RELATIVE_TO_SELF,fromy,
        Animation.RELATIVE_TO_SELF,toy
    )
}
open class Mylistener:Animation.AnimationListener{
    override fun onAnimationStart(animation: Animation?) {
    }

    override fun onAnimationEnd(animation: Animation?) {

    }

    override fun onAnimationRepeat(animation: Animation?) {
    }
}